package guilayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import modellayer.Customer;
import controllayer.CustomerCtrl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CustomerOverview extends JPanel {
	private CustomerCtrl customerCtrl;
	private JTable table;
	private DefaultTableModel tableModel;
	private String tableSelectedCustomerNo;
	private JButton btnLookupCustomer;
	private JButton btnEditCustomer;
	private JButton btnDeletecustomer;

	/**
	 * Create the panel.
	 */
	public CustomerOverview() {
		customerCtrl = CustomerCtrl.getInstance();
		setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);

		createAndSetTable(scrollPane);
		
		createAndSetBottomButtons();
		
		showBottomButtons(false);

	}

	private void createAndSetBottomButtons() {
		JPanel panel = new JPanel();
		// Remove the background
		panel.setOpaque(false);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);
		
		btnLookupCustomer = new JButton("Se kunde");
		btnLookupCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(1, 12, tableSelectedCustomerNo);
			}
		});
		panel.add(btnLookupCustomer);
		
		btnEditCustomer = new JButton("Rediger kunde");
		btnEditCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callRootPane(1, 13, tableSelectedCustomerNo);
			}
		});
		panel.add(btnEditCustomer);
		
		btnDeletecustomer = new JButton("Slet kunde");
		btnDeletecustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//callRootPane(1, 14, tableSelectedCustomerNo);
				buttonActionDeleteCustomer();
			}
		});
		panel.add(btnDeletecustomer);
	}

	private void createAndSetTable(JScrollPane scrollPane) {
		String[] columnNames = { "Kundenr.", "Navn", "Adresse" };
		tableModel = new DefaultTableModel(null, columnNames);
		
		for(Customer customer : customerCtrl.getCustomerArray()) {
			
			String[] rowData = { customer.getCustomerNo(), customer.getFirstName() + " " + customer.getLastName(), customer.getAddressStreet() + ", " + customer.getAddressZipcode() + " " + customer.getAddressCity() };
			
			tableModel.addRow(rowData);
		}
		
		table = new JTable(tableModel) {  
				public boolean isCellEditable(int row,int column){  
					return false;  
				}
			};
			
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				JTable target = (JTable)e.getSource();
		         
		         int row = target.getSelectedRow();
		         
		         tableSelectedCustomerNo = (String) table.getValueAt(row, 0); // Getting the data which is in the "row" number and column 0 (the first).
		         
		         showBottomButtons(true);
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			   public void mouseClicked(MouseEvent e) {
			      if (e.getClickCount() == 2) {
			         JTable target = (JTable)e.getSource();
			         
			         int row = target.getSelectedRow();
			         
			         String customerNo = (String) table.getValueAt(row, 0); // Getting the data which is in the "row" number and column 0 (the first).
			         
			         callRootPane(1, 12, customerNo);
			      }
			   }
			});
		
		scrollPane.setViewportView(table);
	}
	
	private void showBottomButtons(boolean show) {
		btnLookupCustomer.setVisible(show);
		btnEditCustomer.setVisible(show);
		btnDeletecustomer.setVisible(show);
	}
	
	private void buttonActionDeleteCustomer() {
		/*int answer = JOptionPane.showConfirmDialog(this, "Are you sure that you will permanently delete "+table.getValueAt(table.getSelectedRow(), 1)+" as customer?", "Exit", JOptionPane.YES_NO_CANCEL_OPTION);
		if(answer == JOptionPane.OK_OPTION) {
			if (customerCtrl.deleteCustomer(tableSelectedCustomerNo)) {
				((MainWindow)(this.getRootPane().getParent())).refreshAll(1, 1, null);
			}
		}/* else if (answer == JOptionPane.NO_OPTION) {
			
		} else if (JOptionPane.CANCEL_OPTION) {
			// Do nothing.
		}*/
		
		callRootPane(1, 14, tableSelectedCustomerNo);
	}

	
	private void callRootPane(int leftMenu, int mainContent, Object data) {
		((MainWindow)(this.getRootPane().getParent())).refreshAll(leftMenu, mainContent, data);
	}

}
